package main

import (
	"fmt"
	"reflect"
)

type User struct {
	UserID   int `name:"uid" def:"456"`
	UserName string
}

func map2Struct(map2 map[string]interface{}, struct2 interface{}) {
	v := reflect.ValueOf(struct2)
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	} else {
		panic("must ptr")
	}
	for i := 0; i < v.NumField(); i++ {
		var value interface{}
		string := v.Type().Field(i).Name
		tag := v.Type().Field(i).Tag.Get("name")
		value1, ok := map2[tag]
		value2, ok2 := map2[string]
		if ok == true {
			value = value1
		} else if ok2 {
			value = value2
		}
		if (ok || ok2) && v.Field(i).Kind() == reflect.ValueOf(value).Kind() {
			v.Field(i).Set(reflect.ValueOf(value))
		}
	}
}
func main() {
	u := &User{}

	map2 := make(map[string]interface{})
	map2["uid"] = 111
	map2["UserName"] = "huayanzu"
	map2Struct(map2, u)
	fmt.Println(u)
}
